
package com.yuan.control;

import com.yuan.graduate.R;
import com.yuan.prefence.Constants.MARK;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假类型 控制类
 * 
 * @author YUAN
 */
public class leaveTypeManager {
    private static SQLiteDatabase mSearchDataBase = null;
    private static final String SEARCH_DB_NAME = "mark.db";
    private static final String TABLE_NEW_NAME = "MARK";
    private static final String POPULAR_FIELDS[] = new String[] {
            MARK.MARK_ID, MARK.MARK_MONEY, MARK.MARK_ORD,
            MARK.MARK_VALUE, MARK.MARK_MARK
    };

    public static final boolean initMarkSql(Context context) {
        boolean sus = false;
        try {
            File dir = getNewDBFile(context, SEARCH_DB_NAME);
            InputStream is = context.getResources().openRawResource(R.raw.mark);
            FileOutputStream fos = new FileOutputStream(dir);
            byte[] buffer = new byte[8192];
            int count = 0;
            while ((count = is.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
                fos.flush();
            }
            fos.close();
            is.close();
            sus = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sus;
    }

    public final static File getNewDBFile(Context context, String DBName) {
        return new File(context.getDir("yuanwei_graduate", Activity.MODE_PRIVATE), DBName);
    }

    public static SQLiteDatabase getSearchPopularDatabase(Context context) {
        String string = getNewDBFile(context, SEARCH_DB_NAME).getPath();
        return SQLiteDatabase.openOrCreateDatabase(string, null);
    }

    private static Map<String, String> getSuggestItem(Cursor cursor) {
        Map<String, String> item = new HashMap<String, String>();
        item.put(POPULAR_FIELDS[0], cursor.getString(cursor.getColumnIndex(POPULAR_FIELDS[0])));
        item.put(POPULAR_FIELDS[1], cursor.getString(cursor.getColumnIndex(POPULAR_FIELDS[1])));
        item.put(POPULAR_FIELDS[2], cursor.getString(cursor.getColumnIndex(POPULAR_FIELDS[2])));
        item.put(POPULAR_FIELDS[3], cursor.getString(cursor.getColumnIndex(POPULAR_FIELDS[3])));
        item.put(POPULAR_FIELDS[4], cursor.getString(cursor.getColumnIndex(POPULAR_FIELDS[4])));

        return item;
    }

    public static void closeDataBase(Cursor cursor) {
        if (cursor != null && !cursor.isClosed())
            cursor.close();
        if (mSearchDataBase != null && !mSearchDataBase.isOpen()) {
            mSearchDataBase.close();
        }
    }

    @SuppressWarnings("rawtypes")
    public static final synchronized List<Map> queryMarkData(Context context) {
        List<Map> items = new ArrayList<Map>();
        Cursor cursor = null;
        try {
            if (mSearchDataBase == null)
                mSearchDataBase = getSearchPopularDatabase(context);
            cursor = mSearchDataBase.query(TABLE_NEW_NAME, POPULAR_FIELDS, null, null, null,
                    null, POPULAR_FIELDS[0]);
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    items.add(getSuggestItem(cursor));
                }
            }
            // 关闭Cursor以及数据库
            closeDataBase(cursor);
        } catch (Exception e) {
            // 关闭Cursor以及数据库
            closeDataBase(cursor);
        } finally {
            // 关闭Cursor以及数据库
            closeDataBase(cursor);
        }
        return items;
    }
}
